package cn.zl.common.manage.dashboard;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import oshi.SystemInfo;

import java.util.HashMap;
import java.util.Map;

public class DiskDashboard extends BaseDashboard {

    private final Map<String, DiskItem> map = new HashMap<>();

    public DiskDashboard(SystemInfo si) {
        super(si);
    }

    public void add(String path, long total, long usable) {
        DiskItem item = new DiskItem();
        item.path = path;
        item.total = total;
        item.free = usable;
        item.used = total - usable;
        map.put(path, item);
    }

    @Override
    public JSONObject encode() {
        JSONObject json = new JSONObject();
        JSONArray list = new JSONArray();
        map.values().stream().sorted().forEach(diskItem -> {
            JSONObject item = new JSONObject();
            item.put("path", diskItem.path);
            item.put("total", convertGB(diskItem.total));
            item.put("used", convertGB(diskItem.used));
            item.put("free", convertGB(diskItem.free));
            list.add(item);
        });
        json.put("list", list);
        return json;
    }

    public static class DiskItem implements Comparable<DiskItem> {
        public String path;
        public long total;
        public long used;
        public long free;

        @Override
        public int compareTo(DiskItem o) {
            return this.path.compareTo(o.path);
        }

    }
}
